package com.blog.Bard.common.config;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.blog.Bard.common.dao.BaseRepositoryImpl;


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryBard",
        transactionManagerRef="transactionManagerBard",
        basePackages= {"com.blog.Bard.*.dao"},
        repositoryBaseClass = BaseRepositoryImpl.class)
@EntityScan(basePackages = {"com.blog.Bard.*.pojo"})
public class RepositoryConfigBard {
	
	@Autowired
    private JpaProperties jpaProperties;
	
	@Autowired
	private HibernateProperties hibernateProperties;
	
	@Value("${bard-database-platform}")
	private String dialect;

    @Autowired
    @Qualifier("bardDataSource")//需要指明是那个数据源
    private DataSource bardDataSource;
    
    @Primary//标识为主数据源
    @Bean(name = "entityManagerBard")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryFtnSellHouseMP(builder).getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "entityManagerFactoryBard")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryFtnSellHouseMP (EntityManagerFactoryBuilder builder) {
        return builder
                //设置数据源
                .dataSource(bardDataSource)
                //设置数据源属性
                .properties(getVendorProperties())
                //设置实体类所在位置.扫描所有带有 @Entity 注解的类
                .packages("com.blog.Bard.*.pojo")
                // Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
                // Repository就能用它来创建 EntityManager 了,然后Entity就可以针对数据库执行操作
                .persistenceUnit("bardPersistenceUnit")
                .build();

    }

    private Map<String, Object> getVendorProperties() {
//    	Map<String,Object> map = jpaProperties.getProperties(new HibernateSettings());//新版没这方法了
    	
    	Map<String,Object> map  = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
    	
        map.put("hibernate.dialect",dialect);
        return map;
    }

    @Primary
    @Bean(name = "transactionManagerBard")
    PlatformTransactionManager transactionManagerFtnSellHouseMP(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryFtnSellHouseMP(builder).getObject());
    }

}
